/*
 * Copyright 2012-2014 glodon paas All right reserved. This software is the
 * confidential and proprietary information of glodon paas ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with glodon paas.
 */
package com.glodon.paas.monitor.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glodon.paas.job.model.MonitorEventType;
import com.glodon.paas.job.util.DateFormatUtil;
import com.glodon.paas.monitor.server.biz.domain.MonitorStatusEntity;
import com.glodon.paas.monitor.server.biz.service.MonitorStatusService;
import com.glodon.paas.monitor.server.dto.MonitorStatusQueryDTO;

/**
 * @author licx 2013-2-1 下午4:57:24
 */
@Controller
public class ServerController extends BaseController {

	@Autowired
	private MonitorStatusService	monitorStatusService;

	@RequestMapping("/server")
	public String server() {
		return "server";
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/monitor/server", method = RequestMethod.GET)
	public void doServerQueryList(Map model) throws Exception {
		MonitorStatusQueryDTO monitorStatusQueryDTO=new MonitorStatusQueryDTO(MonitorEventType.SERVER_STATUS,lastWeek);
		List<MonitorStatusEntity> monitorStatusList = monitorStatusService.getByMonitorStatusQueryDTO(monitorStatusQueryDTO);
		List<Map<String, String>> status = serverStatusToStatus(monitorStatusList);
		model.put("status", status);
	}

	private List<Map<String, String>> serverStatusToStatus(List<MonitorStatusEntity> monitorStatusList) {
		List<Map<String, String>> status = new ArrayList<Map<String, String>>();
		for (MonitorStatusEntity monitorStatus : monitorStatusList) {
			HashMap<String, String> s = new HashMap<String, String>();
			s.put("address", monitorStatus.getTraceId());
			s.put("startTime", DateFormatUtil.format(monitorStatus.getCreatedTime()));
			s.put("updateTime", DateFormatUtil.format(monitorStatus.getUpdatedTime()));
			if (monitorStatus.getJson() != null) {
				String ip = monitorStatus.getValues().get("ip") == null ? "" : (String) monitorStatus.getValues().get(
						"ip");
				s.put("ip", ip);
				String statusCode = monitorStatus.getValues().get("statusCode") == null ? "" : (String) monitorStatus
						.getValues().get("statusCode");
				s.put("statusCode", statusCode);
				String appName = monitorStatus.getValues().get("appName") == null ? "" : (String) monitorStatus
						.getValues().get("appName");
				s.put("appName", appName);
				String env = monitorStatus.getValues().get("env") == null ? "" : (String) monitorStatus.getValues()
						.get("env");
				s.put("env", env);
				String healthMessage = monitorStatus.getValues().get("healthMessage") == null ? ""
						: (String) monitorStatus.getValues().get("healthMessage");
				s.put("healthMessage", healthMessage);
			}
			status.add(s);
		}
		return status;
	}
}
